package cn.tedu.dimanage.show.webapi.service;

import cn.tedu.dimanage.common.ex.ServiceException;
import cn.tedu.dimanage.common.web.State;
import cn.tedu.dimanage.pojo.dto.ShowDTO;
import cn.tedu.dimanage.pojo.entity.Show;
import cn.tedu.dimanage.pojo.vo.ShowVO;
import cn.tedu.dimanage.show.service.IShowService;
import cn.tedu.dimanage.show.webapi.mapper.ShowMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShowService implements IShowService {

    @Autowired
    private ShowMapper mapper;


    @Override
    public int addNew(ShowDTO showDTO) {
        Show show = new Show();
        BeanUtils.copyProperties(showDTO,show);
        show.setGmtCreate(LocalDateTime.now());
        show.setGmtModified(null);
        int row = mapper.insert(show);
        if (row != 1){
            throw new ServiceException(State.ERR_INSERT,"插入失败,请稍后再试");
        }
        return row;
    }

    @Override
    public List<ShowVO> selectList() {
        List<ShowVO> showVOS = mapper.selectList();
        return showVOS;
    }

    @Override
    public int deleteById(Long id) {
        int row = mapper.deleteById(id);
        if (row != 1){
            throw new ServiceException(State.ERR_DELETE,"删除失败,请稍后再试");
        }

        return row;
    }

}
